package com.chenjun.mysite.module.sys.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.chenjun.mysite.module.sys.servlet.LoginController.IsLogin;

//@WebFilter("/*")
/**
 * servlet 3.0规范 需要jetty9.0以上支持,本阶段暂时不启用 Servlet Filter implementation class
 * LoginSessionFilter
 */
public class LoginSessionFilter implements Filter
{
	
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
	{
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		// 获得请求的URL
		String url = req.getRequestURL().toString();
		System.out.println("来自登陆session'拦截器:->当前url = "+url);
		HttpSession session = req.getSession();
		String isLoginFlag = String.valueOf(session.getAttribute("isLogin"));
		if ( IsLogin.Y.toString().equals(isLoginFlag) )
		{
			
		}
		else
		{
			if( !url.endsWith("login.jsp")
					&& ! url.endsWith("LoginController")
					&& ! url.endsWith(".js")
					&& ! url.endsWith(".css")
					&& ! url.endsWith(".jpg")
					&& ! url.endsWith(".png")
					&& ! url.endsWith(".ico")
					&& ! url.endsWith(".gif")
					&& ! url.contains("/static/"))
			{
				//强制重定向
				res.sendRedirect("./login.jsp");
				return;
			}
		}
		chain.doFilter(request, response);
	}

	public LoginSessionFilter()
	{

	}

	@Override
	public void destroy()
	{

	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException
	{
		
	}

}
